<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
<script>
// 抽象类
class Beverage {
  constructor() {
    // 001 抽象类不允许实例化
    if (new.target === Beverage) {
      throw new Error('Beverage class 不允许实例化');
    }
    this.init()
  }
  // 模板方法--封装了子类的算法框架
  init() {
    this.boilWater()
    this.brew()
    this.pourInCup()
    if(this.customerCondiments()) {
      this.addCondiments()
    }
  }
  boilWater() {
    console.log('把水煮沸')
  }
  brew() {
    throw new Error('子类必须重写 brew 方法')
  }
  pourInCup() {
    throw new Error('子类必须重写 pourInCup 方法')
  }
  addCondiments() {
    throw new Error('子类必须重写 addCondiments 方法')
  }
  customerCondiments() {
    return true // 默认需要调料
  }
}
class CoffeeWithHook extends Beverage {
  constructor() {
    super()
  }
  brew() {
    console.log('用沸水冲泡咖啡')
  }
  pourInCup() {
    console.log('把咖啡倒进杯子')
  }
  addCondiments() {
    console.log('加糖和牛奶')
  }
  customerCondiments() {
    return window.confirm('请问需要调料吗？')
  }
}
let coffeeWithHook = new CoffeeWithHook()
// 泡一杯咖啡
class Coffee extends Beverage {
  constructor() {
    super()
  }
  brew() {
    console.log('用沸水冲泡咖啡')
  }
  pourInCup() {
    console.log('把咖啡倒进杯子')
  }
  addCondiments() {
    console.log('加糖和牛奶')
  }
}
let coffee = new Coffee()

// 泡一壶茶
class Tea extends Beverage {
  constructor() {
    super()
  }
  brew() {
    console.log('用沸水浸泡茶叶')
  }
  pourInCup() {
    console.log('把茶水倒进杯子')
  }
  addCondiments() {
    console.log('加柠檬')
  }
}
let tea = new Tea()
</script>
</html>